#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 crane <crane@crane-pc>
#
# Distributed under terms of the MIT license.


import matplotlib.pyplot as plt
from math import *
from operator import add

plt.axis('equal')


def draw_closed(xs, ys):
    xs.append(xs[0])
    ys.append(ys[0])

    plt.plot(xs, ys)
    plt.show()

def draw_closed_points(points):
    # points.append(points[0])
    # plt.plot(xs, ys)
    # plt.show()
    xs, ys = points_component(points)
    draw_closed(xs, ys)

def points_component(points):
    # xs = [p[0] for p in points]
    xs = []
    ys = []
    for p in points:
        xs.append(p[0])
        ys.append(p[1])

    return xs, ys

def polar_points(degree, length):
    ''' degree是弧度 '''
    return length * cos(degree), length * sin(degree)

def vector_add(a, b):
    return list(map(add, a, b))


class DrawEquilateral:
    def __init__(self, sides=3, side_len=1):
        self.sides = sides
        self.side_len = side_len

        # self.degree = 2 * pi / sides
        self.degree = (sides-2) * pi / sides
        self.comple_degree = pi - self.degree   # 补角

        # 多边形的第0个点
        self.points = [(side_len/2, 0), ]
        # self.init_point = (side_len/2, 0)
        self.init_degree = pi

    def draw(self):
        # 已经有初始点, 计算剩下的点
        for i in range(1, self.sides):
            next_point_vector_degree = self.init_degree - (i-1) * self.comple_degree
            next_point = self.point_axis(i, next_point_vector_degree)
            # self.points.append()
        draw_closed_points(self.points)

    def point_axis(self, i, degree):
        base_point = self.points[i-1]
        next_point_vector = polar_points(degree, self.side_len)
        next_point = vector_add(base_point, next_point_vector)
        self.points.append(next_point)


def main():
    print("start main")
    d = DrawEquilateral(100)
    d.draw()

if __name__ == "__main__":
    main()
